﻿using System.Threading.Tasks;
using System.Net;
using System.IO;
using System;
using Newtonsoft.Json;

namespace Geek7Utils
{
    class CocosTool
    {
        private SimpleHttp httpClient;
        private SettingData settingData;
        public CocosTool(SimpleHttp httpClient, SettingData settingData)
        {
            this.httpClient = httpClient;
            this.settingData = settingData;
        }

        public void BuildPlayable244(HttpListenerContext ctx, string data, HttpMultipartParser hmp)
        {
            try
            {

                if (hmp.Success)
                {
                    string tempDir = settingData.outpath;

                    //保存到本地
                    string ts = Sercert.GetTimeStamp(DateTime.Now).ToString();
                    string fullPath = Path.Combine(tempDir, ts + hmp.Filename);
                    string upZipPath = Path.Combine(tempDir, ts);
                    string folderName = hmp.Filename.Replace(".zip", "");

                    //验证
                    if ( hmp.Parameters.Count >= 2 &&
                         hmp.Parameters.ContainsKey("ts") &&
                         hmp.Parameters.ContainsKey("secret") &&
                         Sercert.Verify(hmp.Parameters["ts"], hmp.Parameters["secret"]) )
                    {

                        var t = Task.Run(async delegate
                        {

                            //写入本地
                            File.WriteAllBytes(fullPath, hmp.FileContents);
                            Program.tempFileQueue.Enqueue(fullPath);//记录新产生的文件 Or 文件夹

                            //等待
                            await Task.Delay(200);

                            //TODO解压
                            ZipHelper.UnZipFile(fullPath, upZipPath);
                            Program.tempFileQueue.Enqueue(upZipPath);//记录新产生的文件 Or 文件夹

                            //TODO处理 转换成试玩
                            string projPath = Path.Combine(upZipPath, folderName);
                            string outPath = Path.Combine(upZipPath, "index.html");
                            ToSimpleHtml(projPath, outPath);

                            //返回下载链接
                            var res = new PlayableResponseData();
                            res.url = $"{Program.settingData.base_url}/{ts}/index.html";
                            res.succeed = true;
                            httpClient.Response(ctx, JsonConvert.SerializeObject(res));
                        });

                        return;
                    }

                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            httpClient.Error(ctx, "FormData Parse Error!");
        }


        public void ToSimpleHtml( string projPath, string outPath )
        {
            PlayableTool cocosTool = new PlayableTool(projPath, outPath);
            cocosTool.Build();
            cocosTool.OutPut();
        }
    }


    [Serializable]
    class PlayableResponseData {
        public bool succeed;
        public string url;
    }
}
